<template>
	<view>
		<u-navbar title="设备添加"></u-navbar>
		<formz ref="formz" :inputs="inputs"></formz>
		<fixedbtn>
			<u-button type="primary" @click="save" :loading="loading">
				{{ $z.$t("添加设备") }}
			</u-button>
		</fixedbtn>
	</view>
</template>

<script>
	// import json from "./indexjson.js";
	export default {
		data() {
			return {
				inputs: [],
				loading: false,
				json: {},
			};
		},
		mounted() {
			this.init();
		},
		methods: {
			async init() {
				await this.$store.dispatch("user/getuser");
				let json = await import("./indexjson.js");
				this.json = json.default;
				this.inputs = json.default.ChargeCaraddjson;
				console.log(this.inputs);
			},
			async save() {
				let data = await this.$refs.formz.validate();
				if (!data.type) {
					data.type = "2";
				}
				if (data.type == "15") {
					data.df = Number(data.dfsj) + Number(data.qbf);
				}
				let errdatas = await this.json.checkdata(this.$u.deepClone(data));
				if (errdatas.length != 0) {
					this.$u.toast(errdatas[0]);
					return;
				}
				if (data.type == "3" || data.type == "8") {
					if (!this.sortkeyfn(data, "a")) {
						await this.msgbox({
							content: this.$z.$t("瓦数没有从低到高排序是否继续？"),
						});
					} else if (!this.sortkeyfn(data, "df", false)) {
						await this.msgbox({
							content: "充电时长没有从高到低排序是否继续？",
						});
					}
				} else if (data.type == "6") {
					if (!this.sortkeyfn(data, "a")) {
						await this.msgbox({
							content: this.$z.$t("瓦数没有从低到高排序是否继续？"),
						});
					} else if (!this.sortkeyfn(data, "df")) {
						await this.msgbox({
							content: this.$z.$t("时长费用没有从低到高排序是否继续？"),
						});
					}
				}
				this.loading = true;
				let par = await this.$z.request("/ChargeCaradd", data);

				console.log(data);
				if (par[0].par == "succeed") {
					if (data.type == "12" || data.type == "14") {
						await this.$z.request("/ChargeCfgdfedt", data);
					}
					uni.$u.toast(this.$z.$t("操作成功"));
					this.$z.back();
				} else {
					let str = config.pars[par[0].par] || "";
					uni.$u.toast(str);
					this.loading = false;
				}
			},
		},
	};
</script>

<style></style>